package edu.ccut.saturn.studio.logic.model.saturn;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.swt.graphics.Image;
import org.w3c.dom.Node;

import edu.ccut.saturn.studio.Activator;
import edu.ccut.saturn.studio.Images;
import edu.ccut.saturn.studio.core.util.eclipse.Messages;
import edu.ccut.saturn.studio.core.util.freemaker.FreemarkerUtils;
import edu.ccut.saturn.studio.core.util.xml.SaturnW3CUtil;
import edu.ccut.saturn.studio.dialog.transformdialog.SaturnTransformDialog;
import edu.ccut.saturn.studio.export.createhtml.Parameters;
import edu.ccut.saturn.studio.logic.figure.SaturnFigure;
import edu.ccut.saturn.studio.logic.model.Shape;

public class SaturnTransformNode extends Shape {

	public static final String SHOW_NODE_TRANSFORM_CONTENTS = "contents";

	public static final String SHOW_NODE_TRANSFORM_DESCRIPTION = "description";

	public static final String SHOW_NODE_TRANSFORM_VALUE_KEY = "value-key";

	public static final String SHOW_NODE_TRANSFORM_TYPE = "type";
	
	public static final String SHOW_NODE_TRANSFORM_NAME = "name";

	private static final long serialVersionUID = -6300588053096396001L;

	public static final String KEY_NODE_TRANSFORM = "transform";
	public static final String SHOW_NODE_TRANSFORM = Messages.getString("SaturnTransformNode.switch"); //$NON-NLS-1$

	public static final String KEY_TRANSFORM_NODE_NAME = SHOW_NODE_TRANSFORM_NAME;

	public static final String KEY_TRANSFORM_NODE_VALUEKEY = SHOW_NODE_TRANSFORM_VALUE_KEY;

	public static final String KEY_TRANSFORM_NODE_TYPE = SHOW_NODE_TRANSFORM_TYPE;

	public static final String KEY_TRANSFORM_NODE_DESCRIPTION = SHOW_NODE_TRANSFORM_DESCRIPTION;

	public static final String KEY_TRANSFORM_NODE_CONTENTS = SHOW_NODE_TRANSFORM_CONTENTS;

	private static int number = 1;

	private String valueKey = "";
	
	private String type = "xml";

	private Image image = Activator.getImage(Images.TRANSFORM_ICON);

	public SaturnTransformNode() {
		super.setName(SHOW_NODE_TRANSFORM + number++);
		super.setSize(new Dimension(50, 50));
	}

	public Image getIcon() {
		return image;
	}

	public IFigure getFigure() {
		SaturnFigure saturnFigure = new SaturnFigure();
		saturnFigure.setIcon(image);
		saturnFigure.setName(super.getName());

		return saturnFigure;
	}

	public String getValueKey() {
		return valueKey;
	}

	public void setValueKey(String valueKey) {
		if (!this.valueKey.equals(valueKey)) {
			this.valueKey = valueKey;
			firePropertyChange(PROPERTERY_CHANGS, null, valueKey);
		}
	}

	public Node doRefresh() {
		Node root = super.getRoot();

		Node transformNode = SaturnW3CUtil.createChildNode(root,
				KEY_NODE_TRANSFORM, null);
		
		SaturnW3CUtil.createChildNode(transformNode,
				SHOW_NODE_TRANSFORM_VALUE_KEY, this.valueKey);
		SaturnW3CUtil.createChildNode(transformNode,
				SHOW_NODE_TRANSFORM_TYPE, getType());
		
		SaturnW3CUtil.createChildNode(transformNode,
				SHOW_NODE_TRANSFORM_NAME, super.getName());

		super.createContents(transformNode);

		return transformNode;
	}

	public Dialog getDialog() {
		SaturnTransformDialog contentDialog = new SaturnTransformDialog(super
				.getShell(), this);
		super.reorder(this);
		return contentDialog;
	}

	public String getKey() {
		return this.valueKey;
	}

	public void setKey(String key) {
		if (!this.valueKey.equals(key)) {
			this.valueKey = key;
			firePropertyChange(PROPERTERY_CHANGS, null, key);
		}
	}

	public Shape cloneShape(Point mousePoint, Shape shape) {
		SaturnTransformNode saturnTransformNode = new SaturnTransformNode();
		SaturnTransformNode saturn = (SaturnTransformNode) shape;
		saturnTransformNode.setLocation(mousePoint);
		saturnTransformNode.setName(saturn.getName());
		saturnTransformNode.setDescription(saturn.getDescription());
		saturnTransformNode.setContents(saturn.getContents());
		saturnTransformNode.setValueKey(saturn.getValueKey());
		saturnTransformNode.setId(saturn.getId());
		saturnTransformNode.setNextId(saturn.getNextId());
		saturnTransformNode.setType(getType());
		return saturnTransformNode;
	}

	protected void setImage(Image image) {
		this.image = image;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		if (!this.type.equals(type)) {
			this.type = type;
			firePropertyChange(PROPERTERY_CHANGS, null, type);
		}
	}

	@Override
	public Parameters createNodeHtml(String filePath, String[] tempnames) {

		List<Parameters> basicvaluse = new ArrayList<Parameters>();
		Map<String, List<?>> summm = new HashMap<String, List<?>>();
		List<SaturnContent> parameters = getContents();
		String name = getName();
		Parameters para = new Parameters();

		para.setName(getName());
		para.setId(getId());
		para.setExperssion(getKey());
		para.setDescription(getDescription());
		para.setTransactiontype(getType());
	 	basicvaluse.add(para);
	 	
		para.setInfourl( name + "-transform.html");
		para.setInfoname( name);
		para.setDescription(getDescription());
		para.setNodeType(Messages.getString("Action.TransferNode")); //$NON-NLS-1$
		para.setImagename("transform");
		summm.put("parameters", parameters);
		summm.put("basicvaluse", basicvaluse);
		String templatePath = "template/export/action/transform";
		String Path = filePath + File.separator + name + "-transform.html";
		FreemarkerUtils.writeTemplateToFile(templatePath, summm, Path);
		
		return para;
	}
}
